//展开更多密码保护资料
function showdetails(o)
{
	if(o.checked == true)
	{
		$("#details").show("fast");
	}
	else
	{
		$("#details").hide("fast");
	}
}


//身份证号码验证自定义函数
function isIdCardNo(num)
{
	var factorArr = new Array(7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2,1);
	var parityBit=new Array("1","0","X","9","8","7","6","5","4","3","2");
	var varArray = new Array();
	var intValue;
	var lngProduct = 0;
	var intCheckDigit;
	var intStrLen = num.length;
	var idNumber = num;
	// initialize
	if ((intStrLen != 15) && (intStrLen != 18)) {
		return false;
	}
	// check and set value
	for(i=0;i<intStrLen;i++) {
		varArray[i] = idNumber.charAt(i);
		if ((varArray[i] < '0' || varArray[i] > '9') && (i != 17)) {
			return false;
		} else if (i < 17) {
			varArray[i] = varArray[i] * factorArr[i];
		}
	}

	if (intStrLen == 18) {
		//check date
		var date8 = idNumber.substring(6,14);
		if (isDate8(date8) == false) {
			return false;
		}
		// calculate the sum of the products
		for(i=0;i<17;i++) {
			lngProduct = lngProduct + varArray[i];
		}
		// calculate the check digit
		intCheckDigit = parityBit[lngProduct % 11];
		// check last digit
		if (varArray[17] != intCheckDigit) {
			return false;
		}
	}
	else{        //length is 15
		//check date
		var date6 = idNumber.substring(6,12);
		if (isDate6(date6) == false) {

			return false;
		}
	}
	return true;
}

//判断是否为“YYYYMM”式的时期
function isDate6(sDate)
{
	if(!/^[0-9]{6}$/.test(sDate))
	{
		return false;
	}
	var year, month, day;
	year = sDate.substring(0, 4);
	month = sDate.substring(4, 6);
	if (year < 1700 || year > 2500) return false
	if (month < 1 || month > 12) return false
	return true
}

//判断是否为“YYYYMMDD”式的时期
function isDate8(sDate) {
	if(!/^[0-9]{8}$/.test(sDate)) {
		return false;
	}
	var year, month, day;
	year = sDate.substring(0, 4);
	month = sDate.substring(4, 6);
	day = sDate.substring(6, 8);
	var iaMonthDays = [31,28,31,30,31,30,31,31,30,31,30,31]
	if (year < 1700 || year > 2500) return false
	if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) iaMonthDays[1]=29;
	if (month < 1 || month > 12) return false
	if (day < 1 || day > iaMonthDays[month - 1]) return false
	return true
}


// 密码强度检察
//CharMode函数
//测试某个字符是属于哪一类.
function CharMode(iN){
	if (iN>=48 && iN <=57) //数字
	return 1;
	if (iN>=65 && iN <=90) //大写字母
	return 2;
	if (iN>=97 && iN <=122) //小写
	return 4;
	else
	return 8;
}

//bitTotal函数
//计算出当前密码当中一共有多少种模式
function bitTotal(num){
	modes=0;
	for (i=0;i<4;i++){
		if (num & 1) modes++;
		num>>>=1;
	}
	return modes;
}

//checkStrong函数
//返回密码的强度级别

function checkStrong(sPW){
	if (sPW.length<=6)
	return 0; //密码太短
	Modes=0;
	for (i=0;i<sPW.length;i++){
		//测试每一个字符的类别并统计一共有多少种模式.

		Modes|=CharMode(sPW.charCodeAt(i));

	}

	return bitTotal(Modes);

}



function pwdStrength(pwd){
	O_color="#eeeeee";
	L_color="#FF0000";
	M_color="#FF9900";
	H_color="#33CC00";
	if (pwd==null||pwd==""){
		Lcolor=Mcolor=Hcolor=O_color;
	}
	else{
		S_level=checkStrong(pwd);
		switch(S_level) {
			case 0:

			Lcolor=Mcolor=Hcolor=O_color;
			case 1:

			Lcolor=L_color;
			Mcolor=Hcolor=O_color;
			break;
			case 2:

			Mcolor=M_color;
			Lcolor=Hcolor=O_color;
			break;
			default:
			Lcolor=Mcolor=O_color;
			Hcolor=H_color;
		}
	}

	document.getElementById("password_L").style.background=Lcolor;
	document.getElementById("password_M").style.background=Mcolor;
	document.getElementById("password_H").style.background=Hcolor;
	return true;
}

/* 追加自定义验证方法 */
// 密码复杂度验证
jQuery.validator.addMethod("pwdStrength", function(value, element) {
	return this.optional(element) || pwdStrength(value);
}, "aaa");

// 身份证号码验证
jQuery.validator.addMethod("idcardno", function(value, element) {
	return this.optional(element) || isIdCardNo(value);
}, "请正确输入身份证号码");

$(document).ready(function() {

	// 用户注册
	$("#form_register").validate({
		rules: {
			username: {
				required: true,
				minlength: 4,
				remote:{
					type: "POST",
					url: "/member/username_exists/",
					data: {
						uername:function(){
							return $(document.getElementById("form_register").uername).val();
						}
					}
				}
			},
			password: {
				required: true,
				minlength: 6,
				pwdStrength:true
			},
			confirm_password: {
				required: true,
				minlength: 6,
				equalTo: "#password"
			},
			name:{
				required: true
			},
			id_number:{
				required:true,
				idcardno:true
			},
			nick:{
				required:true
			},
			confirm_safety_code:{
				equalTo: "#safety_code"
			},
			verifyCode:{
				required: true,
				remote:{
					type: "POST",
					url: "/member/verifycode_validate/",
					data: {
						verifyCode:function(){
							return $(document.getElementById("form_register").verifyCode).val();
						}
					}
				}
			},
			email: {
				email:true
			}
		},
		messages: {
			username: {
				required: "帐号由4-16位大小写英文和数字组合，首位为字母",
				remote:"用户名已经存在",
				minlength: jQuery.format("密码不能小于{0}个字符")
			},
			password: {
				required: "密码可使用英文，数字。长度为8-16的字符",
				minlength: jQuery.format("密码不能小于{0}个字符")
			},
			confirm_password: {
				required: "密码可使用英文，数字。长度为8-16的字符",
				minlength: jQuery.format("密码不能小于{0}个字符"),
				equalTo: "两次输入的密码不一致"
			},
			name:{
				required:"请输入真实姓名"
			},
			id_number:{
				required:"请填写与真实姓名匹配的身份证件",
				idcardno:"请输入正确的身份证号码"
			},
			verifyCode:{
				required:"请输入验证码",
				remote:"验证码不正确"
			},
			confirm_safety_code:{
				equalTo: "两次输入的安全码不一致"
			},
			nick:{
				required:"请输入用户昵称"
			},
			email:{
				email: "请输入正确的Email地址"
			}
		},
		errorPlacement:function(error,element) {
			$(element.parent("p").children("span")).html("");
			error.appendTo(element.parent("p").children("span"));
		},
		success:"valid"
	});

	//ajax提交表单
	$("#form_register").submit(function() {
		if($("#form_register").valid() == true)
		{
			$.ajax({type:"POST", url:"/member/register.html", data:$('#form_register').formSerialize(), dataType:"json", success:function (data){
				if(data.statusCode == 200)
				{
					window.location.href=data.forwardUrl;
				}
				else if(data.statusCode = 300)
				{
					alert(data.message);
				}
				else
				{
					alert("操作出错");
				}
			}});
		}
		return false;
	});
});